package pe.edu.upc.e41a.javasoft.jsf;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import javax.faces.convert.DateTimeConverter;
import javax.faces.validator.LengthValidator;
import org.primefaces.component.autocomplete.AutoComplete;
import org.primefaces.component.calendar.Calendar;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.inputtextarea.InputTextarea;
import org.primefaces.component.message.Message;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.managedbean.RooJsfManagedBean;
import org.springframework.roo.addon.serializable.RooSerializable;
import pe.edu.upc.e41a.javasoft.domain.Aviso;
import pe.edu.upc.e41a.javasoft.domain.Publicacion;
import pe.edu.upc.e41a.javasoft.domain.TipoDocumento;
import pe.edu.upc.e41a.javasoft.domain.TipoPersona;
import pe.edu.upc.e41a.javasoft.domain.Transaccion;
import pe.edu.upc.e41a.javasoft.domain.Usuario;
import pe.edu.upc.e41a.javasoft.jsf.converter.TipoDocumentoConverter;
import pe.edu.upc.e41a.javasoft.jsf.converter.TipoPersonaConverter;
import pe.edu.upc.e41a.javasoft.service.TipoDocumentoService;
import pe.edu.upc.e41a.javasoft.service.TipoPersonaService;
import pe.edu.upc.e41a.javasoft.service.UsuarioService;

@ManagedBean(name = "usuarioBean")
@SessionScoped
@Configurable
@RooSerializable
@RooJsfManagedBean(entity = Usuario.class, beanName = "usuarioBean")
public class UsuarioBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
    UsuarioService usuarioService;

	@Autowired
    TipoPersonaService tipoPersonaService;

	@Autowired
    TipoDocumentoService tipoDocumentoService;

	private String name = "Usuarios";

	private Usuario usuario;

	private List<Usuario> allUsuarios;

	private boolean dataVisible = false;

	private List<String> columns;

	private HtmlPanelGrid createPanelGrid;

	private HtmlPanelGrid editPanelGrid;

	private HtmlPanelGrid viewPanelGrid;

	private boolean createDialogVisible = false;

	private List<Aviso> selectedAvisoes;

	private List<Publicacion> selectedPublicacions;

	private List<Transaccion> selectedTransaccions;

	@PostConstruct
    public void init() {
        columns = new ArrayList<String>();
        columns.add("nombres");
        columns.add("apellidoPaterno");
        columns.add("apellidoMaterno");
        columns.add("sexo");
        columns.add("numeroDocumento");
    }

	public String getName() {
        return name;
    }

	public List<String> getColumns() {
        return columns;
    }

	public List<Usuario> getAllUsuarios() {
        return allUsuarios;
    }

	public void setAllUsuarios(List<Usuario> allUsuarios) {
        this.allUsuarios = allUsuarios;
    }

	public String findAllUsuarios() {
        allUsuarios = usuarioService.findAllUsuarios();
        dataVisible = !allUsuarios.isEmpty();
        return null;
    }

	public boolean isDataVisible() {
        return dataVisible;
    }

	public void setDataVisible(boolean dataVisible) {
        this.dataVisible = dataVisible;
    }

	public HtmlPanelGrid getCreatePanelGrid() {
        if (createPanelGrid == null) {
            createPanelGrid = populateCreatePanel();
        }
        return createPanelGrid;
    }

	public void setCreatePanelGrid(HtmlPanelGrid createPanelGrid) {
        this.createPanelGrid = createPanelGrid;
    }

	public HtmlPanelGrid getEditPanelGrid() {
        if (editPanelGrid == null) {
            editPanelGrid = populateEditPanel();
        }
        return editPanelGrid;
    }

	public void setEditPanelGrid(HtmlPanelGrid editPanelGrid) {
        this.editPanelGrid = editPanelGrid;
    }

	public HtmlPanelGrid getViewPanelGrid() {
        if (viewPanelGrid == null) {
            viewPanelGrid = populateViewPanel();
        }
        return viewPanelGrid;
    }

	public void setViewPanelGrid(HtmlPanelGrid viewPanelGrid) {
        this.viewPanelGrid = viewPanelGrid;
    }

	public HtmlPanelGrid populateCreatePanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText avisoesCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        avisoesCreateOutput.setId("avisoesCreateOutput");
        avisoesCreateOutput.setValue("Avisoes:   ");
        htmlPanelGrid.getChildren().add(avisoesCreateOutput);
        
        HtmlOutputText avisoesCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        avisoesCreateInput.setId("avisoesCreateInput");
        avisoesCreateInput.setValue("This relationship is managed from the Aviso side");
        htmlPanelGrid.getChildren().add(avisoesCreateInput);
        
        Message avisoesCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        avisoesCreateInputMessage.setId("avisoesCreateInputMessage");
        avisoesCreateInputMessage.setFor("avisoesCreateInput");
        avisoesCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(avisoesCreateInputMessage);
        
        HtmlOutputText publicacionsCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        publicacionsCreateOutput.setId("publicacionsCreateOutput");
        publicacionsCreateOutput.setValue("Publicacions:   ");
        htmlPanelGrid.getChildren().add(publicacionsCreateOutput);
        
        HtmlOutputText publicacionsCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        publicacionsCreateInput.setId("publicacionsCreateInput");
        publicacionsCreateInput.setValue("This relationship is managed from the Publicacion side");
        htmlPanelGrid.getChildren().add(publicacionsCreateInput);
        
        Message publicacionsCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        publicacionsCreateInputMessage.setId("publicacionsCreateInputMessage");
        publicacionsCreateInputMessage.setFor("publicacionsCreateInput");
        publicacionsCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(publicacionsCreateInputMessage);
        
        HtmlOutputText transaccionsCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsCreateOutput.setId("transaccionsCreateOutput");
        transaccionsCreateOutput.setValue("Transaccions:   ");
        htmlPanelGrid.getChildren().add(transaccionsCreateOutput);
        
        HtmlOutputText transaccionsCreateInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsCreateInput.setId("transaccionsCreateInput");
        transaccionsCreateInput.setValue("This relationship is managed from the Transaccion side");
        htmlPanelGrid.getChildren().add(transaccionsCreateInput);
        
        Message transaccionsCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        transaccionsCreateInputMessage.setId("transaccionsCreateInputMessage");
        transaccionsCreateInputMessage.setFor("transaccionsCreateInput");
        transaccionsCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(transaccionsCreateInputMessage);
        
        HtmlOutputText tipoPersonaCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoPersonaCreateOutput.setId("tipoPersonaCreateOutput");
        tipoPersonaCreateOutput.setValue("Tipo Persona:   ");
        htmlPanelGrid.getChildren().add(tipoPersonaCreateOutput);
        
        AutoComplete tipoPersonaCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        tipoPersonaCreateInput.setId("tipoPersonaCreateInput");
        tipoPersonaCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.tipoPersona}", TipoPersona.class));
        tipoPersonaCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{usuarioBean.completeTipoPersona}", List.class, new Class[] { String.class }));
        tipoPersonaCreateInput.setDropdown(true);
        tipoPersonaCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "tipoPersona", String.class));
        tipoPersonaCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{tipoPersona.descripcion}", String.class));
        tipoPersonaCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{tipoPersona}", TipoPersona.class));
        tipoPersonaCreateInput.setConverter(new TipoPersonaConverter());
        tipoPersonaCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(tipoPersonaCreateInput);
        
        Message tipoPersonaCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        tipoPersonaCreateInputMessage.setId("tipoPersonaCreateInputMessage");
        tipoPersonaCreateInputMessage.setFor("tipoPersonaCreateInput");
        tipoPersonaCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(tipoPersonaCreateInputMessage);
        
        HtmlOutputText tipoDocumentoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoDocumentoCreateOutput.setId("tipoDocumentoCreateOutput");
        tipoDocumentoCreateOutput.setValue("Tipo Documento:   ");
        htmlPanelGrid.getChildren().add(tipoDocumentoCreateOutput);
        
        AutoComplete tipoDocumentoCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        tipoDocumentoCreateInput.setId("tipoDocumentoCreateInput");
        tipoDocumentoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.tipoDocumento}", TipoDocumento.class));
        tipoDocumentoCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{usuarioBean.completeTipoDocumento}", List.class, new Class[] { String.class }));
        tipoDocumentoCreateInput.setDropdown(true);
        tipoDocumentoCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "tipoDocumento", String.class));
        tipoDocumentoCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{tipoDocumento.descripcion}", String.class));
        tipoDocumentoCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{tipoDocumento}", TipoDocumento.class));
        tipoDocumentoCreateInput.setConverter(new TipoDocumentoConverter());
        tipoDocumentoCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(tipoDocumentoCreateInput);
        
        Message tipoDocumentoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        tipoDocumentoCreateInputMessage.setId("tipoDocumentoCreateInputMessage");
        tipoDocumentoCreateInputMessage.setFor("tipoDocumentoCreateInput");
        tipoDocumentoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(tipoDocumentoCreateInputMessage);
        
        HtmlOutputText nombresCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        nombresCreateOutput.setId("nombresCreateOutput");
        nombresCreateOutput.setValue("Nombres: * ");
        htmlPanelGrid.getChildren().add(nombresCreateOutput);
        
        InputTextarea nombresCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        nombresCreateInput.setId("nombresCreateInput");
        nombresCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.nombres}", String.class));
        LengthValidator nombresCreateInputValidator = new LengthValidator();
        nombresCreateInputValidator.setMaximum(50);
        nombresCreateInput.addValidator(nombresCreateInputValidator);
        nombresCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(nombresCreateInput);
        
        Message nombresCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        nombresCreateInputMessage.setId("nombresCreateInputMessage");
        nombresCreateInputMessage.setFor("nombresCreateInput");
        nombresCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(nombresCreateInputMessage);
        
        HtmlOutputText apellidoPaternoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        apellidoPaternoCreateOutput.setId("apellidoPaternoCreateOutput");
        apellidoPaternoCreateOutput.setValue("Apellido Paterno: * ");
        htmlPanelGrid.getChildren().add(apellidoPaternoCreateOutput);
        
        InputText apellidoPaternoCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        apellidoPaternoCreateInput.setId("apellidoPaternoCreateInput");
        apellidoPaternoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.apellidoPaterno}", String.class));
        LengthValidator apellidoPaternoCreateInputValidator = new LengthValidator();
        apellidoPaternoCreateInputValidator.setMaximum(30);
        apellidoPaternoCreateInput.addValidator(apellidoPaternoCreateInputValidator);
        apellidoPaternoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(apellidoPaternoCreateInput);
        
        Message apellidoPaternoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        apellidoPaternoCreateInputMessage.setId("apellidoPaternoCreateInputMessage");
        apellidoPaternoCreateInputMessage.setFor("apellidoPaternoCreateInput");
        apellidoPaternoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(apellidoPaternoCreateInputMessage);
        
        HtmlOutputText apellidoMaternoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        apellidoMaternoCreateOutput.setId("apellidoMaternoCreateOutput");
        apellidoMaternoCreateOutput.setValue("Apellido Materno: * ");
        htmlPanelGrid.getChildren().add(apellidoMaternoCreateOutput);
        
        InputText apellidoMaternoCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        apellidoMaternoCreateInput.setId("apellidoMaternoCreateInput");
        apellidoMaternoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.apellidoMaterno}", String.class));
        LengthValidator apellidoMaternoCreateInputValidator = new LengthValidator();
        apellidoMaternoCreateInputValidator.setMaximum(30);
        apellidoMaternoCreateInput.addValidator(apellidoMaternoCreateInputValidator);
        apellidoMaternoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(apellidoMaternoCreateInput);
        
        Message apellidoMaternoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        apellidoMaternoCreateInputMessage.setId("apellidoMaternoCreateInputMessage");
        apellidoMaternoCreateInputMessage.setFor("apellidoMaternoCreateInput");
        apellidoMaternoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(apellidoMaternoCreateInputMessage);
        
        HtmlOutputText sexoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        sexoCreateOutput.setId("sexoCreateOutput");
        sexoCreateOutput.setValue("Sexo: * ");
        htmlPanelGrid.getChildren().add(sexoCreateOutput);
        
        InputText sexoCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        sexoCreateInput.setId("sexoCreateInput");
        sexoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.sexo}", Character.class));
        sexoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(sexoCreateInput);
        
        Message sexoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        sexoCreateInputMessage.setId("sexoCreateInputMessage");
        sexoCreateInputMessage.setFor("sexoCreateInput");
        sexoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(sexoCreateInputMessage);
        
        HtmlOutputText numeroDocumentoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        numeroDocumentoCreateOutput.setId("numeroDocumentoCreateOutput");
        numeroDocumentoCreateOutput.setValue("Numero Documento: * ");
        htmlPanelGrid.getChildren().add(numeroDocumentoCreateOutput);
        
        InputText numeroDocumentoCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        numeroDocumentoCreateInput.setId("numeroDocumentoCreateInput");
        numeroDocumentoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.numeroDocumento}", String.class));
        LengthValidator numeroDocumentoCreateInputValidator = new LengthValidator();
        numeroDocumentoCreateInputValidator.setMaximum(15);
        numeroDocumentoCreateInput.addValidator(numeroDocumentoCreateInputValidator);
        numeroDocumentoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(numeroDocumentoCreateInput);
        
        Message numeroDocumentoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        numeroDocumentoCreateInputMessage.setId("numeroDocumentoCreateInputMessage");
        numeroDocumentoCreateInputMessage.setFor("numeroDocumentoCreateInput");
        numeroDocumentoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(numeroDocumentoCreateInputMessage);
        
        HtmlOutputText correoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        correoCreateOutput.setId("correoCreateOutput");
        correoCreateOutput.setValue("Correo: * ");
        htmlPanelGrid.getChildren().add(correoCreateOutput);
        
        InputTextarea correoCreateInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        correoCreateInput.setId("correoCreateInput");
        correoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.correo}", String.class));
        LengthValidator correoCreateInputValidator = new LengthValidator();
        correoCreateInputValidator.setMaximum(50);
        correoCreateInput.addValidator(correoCreateInputValidator);
        correoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(correoCreateInput);
        
        Message correoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        correoCreateInputMessage.setId("correoCreateInputMessage");
        correoCreateInputMessage.setFor("correoCreateInput");
        correoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(correoCreateInputMessage);
        
        HtmlOutputText telefonoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        telefonoCreateOutput.setId("telefonoCreateOutput");
        telefonoCreateOutput.setValue("Telefono: * ");
        htmlPanelGrid.getChildren().add(telefonoCreateOutput);
        
        InputText telefonoCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        telefonoCreateInput.setId("telefonoCreateInput");
        telefonoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.telefono}", String.class));
        LengthValidator telefonoCreateInputValidator = new LengthValidator();
        telefonoCreateInputValidator.setMaximum(20);
        telefonoCreateInput.addValidator(telefonoCreateInputValidator);
        telefonoCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(telefonoCreateInput);
        
        Message telefonoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        telefonoCreateInputMessage.setId("telefonoCreateInputMessage");
        telefonoCreateInputMessage.setFor("telefonoCreateInput");
        telefonoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(telefonoCreateInputMessage);
        
        HtmlOutputText fechaNacimientoCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        fechaNacimientoCreateOutput.setId("fechaNacimientoCreateOutput");
        fechaNacimientoCreateOutput.setValue("Fecha Nacimiento:   ");
        htmlPanelGrid.getChildren().add(fechaNacimientoCreateOutput);
        
        Calendar fechaNacimientoCreateInput = (Calendar) application.createComponent(Calendar.COMPONENT_TYPE);
        fechaNacimientoCreateInput.setId("fechaNacimientoCreateInput");
        fechaNacimientoCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.fechaNacimiento}", Date.class));
        fechaNacimientoCreateInput.setNavigator(true);
        fechaNacimientoCreateInput.setEffect("slideDown");
        fechaNacimientoCreateInput.setPattern("dd/MM/yyyy");
        fechaNacimientoCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(fechaNacimientoCreateInput);
        
        Message fechaNacimientoCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        fechaNacimientoCreateInputMessage.setId("fechaNacimientoCreateInputMessage");
        fechaNacimientoCreateInputMessage.setFor("fechaNacimientoCreateInput");
        fechaNacimientoCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(fechaNacimientoCreateInputMessage);
        
        HtmlOutputText usuarioCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuarioCreateOutput.setId("usuarioCreateOutput");
        usuarioCreateOutput.setValue("Usuario: * ");
        htmlPanelGrid.getChildren().add(usuarioCreateOutput);
        
        InputText usuarioCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        usuarioCreateInput.setId("usuarioCreateInput");
        usuarioCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.usuario}", String.class));
        LengthValidator usuarioCreateInputValidator = new LengthValidator();
        usuarioCreateInputValidator.setMaximum(20);
        usuarioCreateInput.addValidator(usuarioCreateInputValidator);
        usuarioCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(usuarioCreateInput);
        
        Message usuarioCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        usuarioCreateInputMessage.setId("usuarioCreateInputMessage");
        usuarioCreateInputMessage.setFor("usuarioCreateInput");
        usuarioCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(usuarioCreateInputMessage);
        
        HtmlOutputText claveCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        claveCreateOutput.setId("claveCreateOutput");
        claveCreateOutput.setValue("Clave: * ");
        htmlPanelGrid.getChildren().add(claveCreateOutput);
        
        InputText claveCreateInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        claveCreateInput.setId("claveCreateInput");
        claveCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.clave}", String.class));
        LengthValidator claveCreateInputValidator = new LengthValidator();
        claveCreateInputValidator.setMaximum(20);
        claveCreateInput.addValidator(claveCreateInputValidator);
        claveCreateInput.setRequired(true);
        htmlPanelGrid.getChildren().add(claveCreateInput);
        
        Message claveCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        claveCreateInputMessage.setId("claveCreateInputMessage");
        claveCreateInputMessage.setFor("claveCreateInput");
        claveCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(claveCreateInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateEditPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText avisoesEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        avisoesEditOutput.setId("avisoesEditOutput");
        avisoesEditOutput.setValue("Avisoes:   ");
        htmlPanelGrid.getChildren().add(avisoesEditOutput);
        
        HtmlOutputText avisoesEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        avisoesEditInput.setId("avisoesEditInput");
        avisoesEditInput.setValue("This relationship is managed from the Aviso side");
        htmlPanelGrid.getChildren().add(avisoesEditInput);
        
        Message avisoesEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        avisoesEditInputMessage.setId("avisoesEditInputMessage");
        avisoesEditInputMessage.setFor("avisoesEditInput");
        avisoesEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(avisoesEditInputMessage);
        
        HtmlOutputText publicacionsEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        publicacionsEditOutput.setId("publicacionsEditOutput");
        publicacionsEditOutput.setValue("Publicacions:   ");
        htmlPanelGrid.getChildren().add(publicacionsEditOutput);
        
        HtmlOutputText publicacionsEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        publicacionsEditInput.setId("publicacionsEditInput");
        publicacionsEditInput.setValue("This relationship is managed from the Publicacion side");
        htmlPanelGrid.getChildren().add(publicacionsEditInput);
        
        Message publicacionsEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        publicacionsEditInputMessage.setId("publicacionsEditInputMessage");
        publicacionsEditInputMessage.setFor("publicacionsEditInput");
        publicacionsEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(publicacionsEditInputMessage);
        
        HtmlOutputText transaccionsEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsEditOutput.setId("transaccionsEditOutput");
        transaccionsEditOutput.setValue("Transaccions:   ");
        htmlPanelGrid.getChildren().add(transaccionsEditOutput);
        
        HtmlOutputText transaccionsEditInput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsEditInput.setId("transaccionsEditInput");
        transaccionsEditInput.setValue("This relationship is managed from the Transaccion side");
        htmlPanelGrid.getChildren().add(transaccionsEditInput);
        
        Message transaccionsEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        transaccionsEditInputMessage.setId("transaccionsEditInputMessage");
        transaccionsEditInputMessage.setFor("transaccionsEditInput");
        transaccionsEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(transaccionsEditInputMessage);
        
        HtmlOutputText tipoPersonaEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoPersonaEditOutput.setId("tipoPersonaEditOutput");
        tipoPersonaEditOutput.setValue("Tipo Persona:   ");
        htmlPanelGrid.getChildren().add(tipoPersonaEditOutput);
        
        AutoComplete tipoPersonaEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        tipoPersonaEditInput.setId("tipoPersonaEditInput");
        tipoPersonaEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.tipoPersona}", TipoPersona.class));
        tipoPersonaEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{usuarioBean.completeTipoPersona}", List.class, new Class[] { String.class }));
        tipoPersonaEditInput.setDropdown(true);
        tipoPersonaEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "tipoPersona", String.class));
        tipoPersonaEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{tipoPersona.descripcion}", String.class));
        tipoPersonaEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{tipoPersona}", TipoPersona.class));
        tipoPersonaEditInput.setConverter(new TipoPersonaConverter());
        tipoPersonaEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(tipoPersonaEditInput);
        
        Message tipoPersonaEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        tipoPersonaEditInputMessage.setId("tipoPersonaEditInputMessage");
        tipoPersonaEditInputMessage.setFor("tipoPersonaEditInput");
        tipoPersonaEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(tipoPersonaEditInputMessage);
        
        HtmlOutputText tipoDocumentoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoDocumentoEditOutput.setId("tipoDocumentoEditOutput");
        tipoDocumentoEditOutput.setValue("Tipo Documento:   ");
        htmlPanelGrid.getChildren().add(tipoDocumentoEditOutput);
        
        AutoComplete tipoDocumentoEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        tipoDocumentoEditInput.setId("tipoDocumentoEditInput");
        tipoDocumentoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.tipoDocumento}", TipoDocumento.class));
        tipoDocumentoEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{usuarioBean.completeTipoDocumento}", List.class, new Class[] { String.class }));
        tipoDocumentoEditInput.setDropdown(true);
        tipoDocumentoEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "tipoDocumento", String.class));
        tipoDocumentoEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{tipoDocumento.descripcion}", String.class));
        tipoDocumentoEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{tipoDocumento}", TipoDocumento.class));
        tipoDocumentoEditInput.setConverter(new TipoDocumentoConverter());
        tipoDocumentoEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(tipoDocumentoEditInput);
        
        Message tipoDocumentoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        tipoDocumentoEditInputMessage.setId("tipoDocumentoEditInputMessage");
        tipoDocumentoEditInputMessage.setFor("tipoDocumentoEditInput");
        tipoDocumentoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(tipoDocumentoEditInputMessage);
        
        HtmlOutputText nombresEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        nombresEditOutput.setId("nombresEditOutput");
        nombresEditOutput.setValue("Nombres: * ");
        htmlPanelGrid.getChildren().add(nombresEditOutput);
        
        InputTextarea nombresEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        nombresEditInput.setId("nombresEditInput");
        nombresEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.nombres}", String.class));
        LengthValidator nombresEditInputValidator = new LengthValidator();
        nombresEditInputValidator.setMaximum(50);
        nombresEditInput.addValidator(nombresEditInputValidator);
        nombresEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(nombresEditInput);
        
        Message nombresEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        nombresEditInputMessage.setId("nombresEditInputMessage");
        nombresEditInputMessage.setFor("nombresEditInput");
        nombresEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(nombresEditInputMessage);
        
        HtmlOutputText apellidoPaternoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        apellidoPaternoEditOutput.setId("apellidoPaternoEditOutput");
        apellidoPaternoEditOutput.setValue("Apellido Paterno: * ");
        htmlPanelGrid.getChildren().add(apellidoPaternoEditOutput);
        
        InputText apellidoPaternoEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        apellidoPaternoEditInput.setId("apellidoPaternoEditInput");
        apellidoPaternoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.apellidoPaterno}", String.class));
        LengthValidator apellidoPaternoEditInputValidator = new LengthValidator();
        apellidoPaternoEditInputValidator.setMaximum(30);
        apellidoPaternoEditInput.addValidator(apellidoPaternoEditInputValidator);
        apellidoPaternoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(apellidoPaternoEditInput);
        
        Message apellidoPaternoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        apellidoPaternoEditInputMessage.setId("apellidoPaternoEditInputMessage");
        apellidoPaternoEditInputMessage.setFor("apellidoPaternoEditInput");
        apellidoPaternoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(apellidoPaternoEditInputMessage);
        
        HtmlOutputText apellidoMaternoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        apellidoMaternoEditOutput.setId("apellidoMaternoEditOutput");
        apellidoMaternoEditOutput.setValue("Apellido Materno: * ");
        htmlPanelGrid.getChildren().add(apellidoMaternoEditOutput);
        
        InputText apellidoMaternoEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        apellidoMaternoEditInput.setId("apellidoMaternoEditInput");
        apellidoMaternoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.apellidoMaterno}", String.class));
        LengthValidator apellidoMaternoEditInputValidator = new LengthValidator();
        apellidoMaternoEditInputValidator.setMaximum(30);
        apellidoMaternoEditInput.addValidator(apellidoMaternoEditInputValidator);
        apellidoMaternoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(apellidoMaternoEditInput);
        
        Message apellidoMaternoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        apellidoMaternoEditInputMessage.setId("apellidoMaternoEditInputMessage");
        apellidoMaternoEditInputMessage.setFor("apellidoMaternoEditInput");
        apellidoMaternoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(apellidoMaternoEditInputMessage);
        
        HtmlOutputText sexoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        sexoEditOutput.setId("sexoEditOutput");
        sexoEditOutput.setValue("Sexo: * ");
        htmlPanelGrid.getChildren().add(sexoEditOutput);
        
        InputText sexoEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        sexoEditInput.setId("sexoEditInput");
        sexoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.sexo}", Character.class));
        sexoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(sexoEditInput);
        
        Message sexoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        sexoEditInputMessage.setId("sexoEditInputMessage");
        sexoEditInputMessage.setFor("sexoEditInput");
        sexoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(sexoEditInputMessage);
        
        HtmlOutputText numeroDocumentoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        numeroDocumentoEditOutput.setId("numeroDocumentoEditOutput");
        numeroDocumentoEditOutput.setValue("Numero Documento: * ");
        htmlPanelGrid.getChildren().add(numeroDocumentoEditOutput);
        
        InputText numeroDocumentoEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        numeroDocumentoEditInput.setId("numeroDocumentoEditInput");
        numeroDocumentoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.numeroDocumento}", String.class));
        LengthValidator numeroDocumentoEditInputValidator = new LengthValidator();
        numeroDocumentoEditInputValidator.setMaximum(15);
        numeroDocumentoEditInput.addValidator(numeroDocumentoEditInputValidator);
        numeroDocumentoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(numeroDocumentoEditInput);
        
        Message numeroDocumentoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        numeroDocumentoEditInputMessage.setId("numeroDocumentoEditInputMessage");
        numeroDocumentoEditInputMessage.setFor("numeroDocumentoEditInput");
        numeroDocumentoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(numeroDocumentoEditInputMessage);
        
        HtmlOutputText correoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        correoEditOutput.setId("correoEditOutput");
        correoEditOutput.setValue("Correo: * ");
        htmlPanelGrid.getChildren().add(correoEditOutput);
        
        InputTextarea correoEditInput = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        correoEditInput.setId("correoEditInput");
        correoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.correo}", String.class));
        LengthValidator correoEditInputValidator = new LengthValidator();
        correoEditInputValidator.setMaximum(50);
        correoEditInput.addValidator(correoEditInputValidator);
        correoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(correoEditInput);
        
        Message correoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        correoEditInputMessage.setId("correoEditInputMessage");
        correoEditInputMessage.setFor("correoEditInput");
        correoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(correoEditInputMessage);
        
        HtmlOutputText telefonoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        telefonoEditOutput.setId("telefonoEditOutput");
        telefonoEditOutput.setValue("Telefono: * ");
        htmlPanelGrid.getChildren().add(telefonoEditOutput);
        
        InputText telefonoEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        telefonoEditInput.setId("telefonoEditInput");
        telefonoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.telefono}", String.class));
        LengthValidator telefonoEditInputValidator = new LengthValidator();
        telefonoEditInputValidator.setMaximum(20);
        telefonoEditInput.addValidator(telefonoEditInputValidator);
        telefonoEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(telefonoEditInput);
        
        Message telefonoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        telefonoEditInputMessage.setId("telefonoEditInputMessage");
        telefonoEditInputMessage.setFor("telefonoEditInput");
        telefonoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(telefonoEditInputMessage);
        
        HtmlOutputText fechaNacimientoEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        fechaNacimientoEditOutput.setId("fechaNacimientoEditOutput");
        fechaNacimientoEditOutput.setValue("Fecha Nacimiento:   ");
        htmlPanelGrid.getChildren().add(fechaNacimientoEditOutput);
        
        Calendar fechaNacimientoEditInput = (Calendar) application.createComponent(Calendar.COMPONENT_TYPE);
        fechaNacimientoEditInput.setId("fechaNacimientoEditInput");
        fechaNacimientoEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.fechaNacimiento}", Date.class));
        fechaNacimientoEditInput.setNavigator(true);
        fechaNacimientoEditInput.setEffect("slideDown");
        fechaNacimientoEditInput.setPattern("dd/MM/yyyy");
        fechaNacimientoEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(fechaNacimientoEditInput);
        
        Message fechaNacimientoEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        fechaNacimientoEditInputMessage.setId("fechaNacimientoEditInputMessage");
        fechaNacimientoEditInputMessage.setFor("fechaNacimientoEditInput");
        fechaNacimientoEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(fechaNacimientoEditInputMessage);
        
        HtmlOutputText usuarioEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuarioEditOutput.setId("usuarioEditOutput");
        usuarioEditOutput.setValue("Usuario: * ");
        htmlPanelGrid.getChildren().add(usuarioEditOutput);
        
        InputText usuarioEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        usuarioEditInput.setId("usuarioEditInput");
        usuarioEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.usuario}", String.class));
        LengthValidator usuarioEditInputValidator = new LengthValidator();
        usuarioEditInputValidator.setMaximum(20);
        usuarioEditInput.addValidator(usuarioEditInputValidator);
        usuarioEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(usuarioEditInput);
        
        Message usuarioEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        usuarioEditInputMessage.setId("usuarioEditInputMessage");
        usuarioEditInputMessage.setFor("usuarioEditInput");
        usuarioEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(usuarioEditInputMessage);
        
        HtmlOutputText claveEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        claveEditOutput.setId("claveEditOutput");
        claveEditOutput.setValue("Clave: * ");
        htmlPanelGrid.getChildren().add(claveEditOutput);
        
        InputText claveEditInput = (InputText) application.createComponent(InputText.COMPONENT_TYPE);
        claveEditInput.setId("claveEditInput");
        claveEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.clave}", String.class));
        LengthValidator claveEditInputValidator = new LengthValidator();
        claveEditInputValidator.setMaximum(20);
        claveEditInput.addValidator(claveEditInputValidator);
        claveEditInput.setRequired(true);
        htmlPanelGrid.getChildren().add(claveEditInput);
        
        Message claveEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        claveEditInputMessage.setId("claveEditInputMessage");
        claveEditInputMessage.setFor("claveEditInput");
        claveEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(claveEditInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateViewPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText avisoesLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        avisoesLabel.setId("avisoesLabel");
        avisoesLabel.setValue("Avisoes:   ");
        htmlPanelGrid.getChildren().add(avisoesLabel);
        
        HtmlOutputText avisoesValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        avisoesValue.setId("avisoesValue");
        avisoesValue.setValue("This relationship is managed from the Aviso side");
        htmlPanelGrid.getChildren().add(avisoesValue);
        
        HtmlOutputText publicacionsLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        publicacionsLabel.setId("publicacionsLabel");
        publicacionsLabel.setValue("Publicacions:   ");
        htmlPanelGrid.getChildren().add(publicacionsLabel);
        
        HtmlOutputText publicacionsValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        publicacionsValue.setId("publicacionsValue");
        publicacionsValue.setValue("This relationship is managed from the Publicacion side");
        htmlPanelGrid.getChildren().add(publicacionsValue);
        
        HtmlOutputText transaccionsLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsLabel.setId("transaccionsLabel");
        transaccionsLabel.setValue("Transaccions:   ");
        htmlPanelGrid.getChildren().add(transaccionsLabel);
        
        HtmlOutputText transaccionsValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        transaccionsValue.setId("transaccionsValue");
        transaccionsValue.setValue("This relationship is managed from the Transaccion side");
        htmlPanelGrid.getChildren().add(transaccionsValue);
        
        HtmlOutputText tipoPersonaLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoPersonaLabel.setId("tipoPersonaLabel");
        tipoPersonaLabel.setValue("Tipo Persona:   ");
        htmlPanelGrid.getChildren().add(tipoPersonaLabel);
        
        HtmlOutputText tipoPersonaValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoPersonaValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.tipoPersona}", TipoPersona.class));
        tipoPersonaValue.setConverter(new TipoPersonaConverter());
        htmlPanelGrid.getChildren().add(tipoPersonaValue);
        
        HtmlOutputText tipoDocumentoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoDocumentoLabel.setId("tipoDocumentoLabel");
        tipoDocumentoLabel.setValue("Tipo Documento:   ");
        htmlPanelGrid.getChildren().add(tipoDocumentoLabel);
        
        HtmlOutputText tipoDocumentoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        tipoDocumentoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.tipoDocumento}", TipoDocumento.class));
        tipoDocumentoValue.setConverter(new TipoDocumentoConverter());
        htmlPanelGrid.getChildren().add(tipoDocumentoValue);
        
        HtmlOutputText nombresLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        nombresLabel.setId("nombresLabel");
        nombresLabel.setValue("Nombres:   ");
        htmlPanelGrid.getChildren().add(nombresLabel);
        
        InputTextarea nombresValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        nombresValue.setId("nombresValue");
        nombresValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.nombres}", String.class));
        nombresValue.setReadonly(true);
        nombresValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(nombresValue);
        
        HtmlOutputText apellidoPaternoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        apellidoPaternoLabel.setId("apellidoPaternoLabel");
        apellidoPaternoLabel.setValue("Apellido Paterno:   ");
        htmlPanelGrid.getChildren().add(apellidoPaternoLabel);
        
        HtmlOutputText apellidoPaternoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        apellidoPaternoValue.setId("apellidoPaternoValue");
        apellidoPaternoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.apellidoPaterno}", String.class));
        htmlPanelGrid.getChildren().add(apellidoPaternoValue);
        
        HtmlOutputText apellidoMaternoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        apellidoMaternoLabel.setId("apellidoMaternoLabel");
        apellidoMaternoLabel.setValue("Apellido Materno:   ");
        htmlPanelGrid.getChildren().add(apellidoMaternoLabel);
        
        HtmlOutputText apellidoMaternoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        apellidoMaternoValue.setId("apellidoMaternoValue");
        apellidoMaternoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.apellidoMaterno}", String.class));
        htmlPanelGrid.getChildren().add(apellidoMaternoValue);
        
        HtmlOutputText sexoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        sexoLabel.setId("sexoLabel");
        sexoLabel.setValue("Sexo:   ");
        htmlPanelGrid.getChildren().add(sexoLabel);
        
        HtmlOutputText sexoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        sexoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.sexo}", String.class));
        htmlPanelGrid.getChildren().add(sexoValue);
        
        HtmlOutputText numeroDocumentoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        numeroDocumentoLabel.setId("numeroDocumentoLabel");
        numeroDocumentoLabel.setValue("Numero Documento:   ");
        htmlPanelGrid.getChildren().add(numeroDocumentoLabel);
        
        HtmlOutputText numeroDocumentoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        numeroDocumentoValue.setId("numeroDocumentoValue");
        numeroDocumentoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.numeroDocumento}", String.class));
        htmlPanelGrid.getChildren().add(numeroDocumentoValue);
        
        HtmlOutputText correoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        correoLabel.setId("correoLabel");
        correoLabel.setValue("Correo:   ");
        htmlPanelGrid.getChildren().add(correoLabel);
        
        InputTextarea correoValue = (InputTextarea) application.createComponent(InputTextarea.COMPONENT_TYPE);
        correoValue.setId("correoValue");
        correoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.correo}", String.class));
        correoValue.setReadonly(true);
        correoValue.setDisabled(true);
        htmlPanelGrid.getChildren().add(correoValue);
        
        HtmlOutputText telefonoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        telefonoLabel.setId("telefonoLabel");
        telefonoLabel.setValue("Telefono:   ");
        htmlPanelGrid.getChildren().add(telefonoLabel);
        
        HtmlOutputText telefonoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        telefonoValue.setId("telefonoValue");
        telefonoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.telefono}", String.class));
        htmlPanelGrid.getChildren().add(telefonoValue);
        
        HtmlOutputText fechaNacimientoLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        fechaNacimientoLabel.setId("fechaNacimientoLabel");
        fechaNacimientoLabel.setValue("Fecha Nacimiento:   ");
        htmlPanelGrid.getChildren().add(fechaNacimientoLabel);
        
        HtmlOutputText fechaNacimientoValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        fechaNacimientoValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.fechaNacimiento}", Date.class));
        DateTimeConverter fechaNacimientoValueConverter = (DateTimeConverter) application.createConverter(DateTimeConverter.CONVERTER_ID);
        fechaNacimientoValueConverter.setPattern("dd/MM/yyyy");
        fechaNacimientoValue.setConverter(fechaNacimientoValueConverter);
        htmlPanelGrid.getChildren().add(fechaNacimientoValue);
        
        HtmlOutputText usuarioLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuarioLabel.setId("usuarioLabel");
        usuarioLabel.setValue("Usuario:   ");
        htmlPanelGrid.getChildren().add(usuarioLabel);
        
        HtmlOutputText usuarioValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        usuarioValue.setId("usuarioValue");
        usuarioValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.usuario}", String.class));
        htmlPanelGrid.getChildren().add(usuarioValue);
        
        HtmlOutputText claveLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        claveLabel.setId("claveLabel");
        claveLabel.setValue("Clave:   ");
        htmlPanelGrid.getChildren().add(claveLabel);
        
        HtmlOutputText claveValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        claveValue.setId("claveValue");
        claveValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{usuarioBean.usuario.clave}", String.class));
        htmlPanelGrid.getChildren().add(claveValue);
        
        return htmlPanelGrid;
    }

	public Usuario getUsuario() {
        if (usuario == null) {
            usuario = new Usuario();
        }
        return usuario;
    }

	public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

	public List<Aviso> getSelectedAvisoes() {
        return selectedAvisoes;
    }

	public void setSelectedAvisoes(List<Aviso> selectedAvisoes) {
        if (selectedAvisoes != null) {
            usuario.setAvisoes(new HashSet<Aviso>(selectedAvisoes));
        }
        this.selectedAvisoes = selectedAvisoes;
    }

	public List<Publicacion> getSelectedPublicacions() {
        return selectedPublicacions;
    }

	public void setSelectedPublicacions(List<Publicacion> selectedPublicacions) {
        if (selectedPublicacions != null) {
            usuario.setPublicacions(new HashSet<Publicacion>(selectedPublicacions));
        }
        this.selectedPublicacions = selectedPublicacions;
    }

	public List<Transaccion> getSelectedTransaccions() {
        return selectedTransaccions;
    }

	public void setSelectedTransaccions(List<Transaccion> selectedTransaccions) {
        if (selectedTransaccions != null) {
            usuario.setTransaccions(new HashSet<Transaccion>(selectedTransaccions));
        }
        this.selectedTransaccions = selectedTransaccions;
    }

	public List<TipoPersona> completeTipoPersona(String query) {
        List<TipoPersona> suggestions = new ArrayList<TipoPersona>();
        for (TipoPersona tipoPersona : tipoPersonaService.findAllTipoPersonas()) {
            String tipoPersonaStr = String.valueOf(tipoPersona.getDescripcion());
            if (tipoPersonaStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(tipoPersona);
            }
        }
        return suggestions;
    }

	public List<TipoDocumento> completeTipoDocumento(String query) {
        List<TipoDocumento> suggestions = new ArrayList<TipoDocumento>();
        for (TipoDocumento tipoDocumento : tipoDocumentoService.findAllTipoDocumentoes()) {
            String tipoDocumentoStr = String.valueOf(tipoDocumento.getDescripcion());
            if (tipoDocumentoStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(tipoDocumento);
            }
        }
        return suggestions;
    }

	public String onEdit() {
        if (usuario != null && usuario.getAvisoes() != null) {
            selectedAvisoes = new ArrayList<Aviso>(usuario.getAvisoes());
        }
        if (usuario != null && usuario.getPublicacions() != null) {
            selectedPublicacions = new ArrayList<Publicacion>(usuario.getPublicacions());
        }
        if (usuario != null && usuario.getTransaccions() != null) {
            selectedTransaccions = new ArrayList<Transaccion>(usuario.getTransaccions());
        }
        return null;
    }

	public boolean isCreateDialogVisible() {
        return createDialogVisible;
    }

	public void setCreateDialogVisible(boolean createDialogVisible) {
        this.createDialogVisible = createDialogVisible;
    }

	public String displayList() {
        createDialogVisible = false;
        findAllUsuarios();
        return "usuario";
    }

	public String displayCreateDialog() {
        usuario = new Usuario();
        createDialogVisible = true;
        return "usuario";
    }

	public String persist() {
        String message = "";
        if (usuario.getCodigo() != null) {
            usuarioService.updateUsuario(usuario);
            message = "Successfully updated";
        } else {
            usuarioService.saveUsuario(usuario);
            message = "Successfully created";
        }
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("createDialog.hide()");
        context.execute("editDialog.hide()");
        
        FacesMessage facesMessage = new FacesMessage(message);
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllUsuarios();
    }

	public String delete() {
        usuarioService.deleteUsuario(usuario);
        FacesMessage facesMessage = new FacesMessage("Successfully deleted");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllUsuarios();
    }

	public void reset() {
        usuario = null;
        selectedAvisoes = null;
        selectedPublicacions = null;
        selectedTransaccions = null;
        createDialogVisible = false;
    }

	public void handleDialogClose(CloseEvent event) {
        reset();
    }
}
